package com.wenhai.java.netty.decode;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;

import java.util.List;

public class MessagePackDecoder extends MessageToMessageDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        MessagePack msgPack = new MessagePack();
        final byte[] raw;
        raw = new byte[msg.readableBytes()];
        msg.readBytes(raw);
        out.add(msgPack.read(raw));
    }
}
